﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using AFT.Parking.Foundation;

namespace AFT.Parking.WebSite.Common
{
    public static class PageHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pagelist"></param>
        /// <returns></returns>
        public static MvcHtmlString Pager(this HtmlHelper html, IPaged pagelist)
        {
            return Pager(html, pagelist.PageIndex, pagelist.PageSize, pagelist.TotalCount);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="html"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static MvcHtmlString Pager(this HtmlHelper html, int pageIndex, int pageSize, int totalCount)
        {
            string currentPageStr = "page";
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
           // var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数  
            var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
            var sb = new StringBuilder();


            foreach (string key in queryString.Keys)
                if (queryString[key] != null && !string.IsNullOrEmpty(key))
                    dict[key] = queryString[key];

            //保留查询字符到下一页
            foreach (string key in queryString.Keys)
                dict[key] = queryString[key];

            if (totalPages > 1)
            {
                dict[currentPageStr] = 1;
                sb.AppendFormat("<span class='number'>{0}</span>", html.RouteLink("首页", dict));

                if (pageIndex == 1)
                {
                    sb.AppendFormat("<span class='number'>{0}</span>", "上一页");
                }
                else
                {
                    dict[currentPageStr] = pageIndex - 1;
                    sb.AppendFormat("<span class='number'>{0}</span>", html.RouteLink("上一页", dict));
                }

                //  sb.Append("<div>");
                //当前页小于6，显示1到10页
                if (pageIndex < 6)
                {
                    var p = totalPages > 10 ? 10 : totalPages;
                    for (int i = 1; i <= p; i++)
                    {
                        var cur = pageIndex == i ? "number current" : "number";
                        dict[currentPageStr] = i;
                        sb.AppendFormat("<span class='{0}'>{1}</span>", cur, html.RouteLink(i.ToString(), dict));
                    }
                    if (totalPages > 10)
                    {
                        sb.Append("<span>...</span>");
                    }
                }
                else //分页数大于等于6
                {
                    if (totalPages > 10)
                    {
                        sb.Append("<span>...</span>");
                    }

                    //显示当前页前后五页，若已到最后10页，显示最后10页
                    var p1 = (pageIndex + 4) >= totalPages ? (totalPages - 10 > 0 ? totalPages - 10 : 1) : pageIndex - 5;
                    var p2 = (pageIndex + 4) >= totalPages ? totalPages : pageIndex + 4;
                    for (int i = p1; i <= p2; i++)
                    {
                        var cur = pageIndex == i ? "number current" : "number";
                        dict[currentPageStr] = i;
                        sb.AppendFormat("<span class='{0}'>{1}</span>", cur, html.RouteLink(i.ToString(), dict));
                    }
                    if (pageIndex + 4 < totalPages)
                    {
                        sb.Append("<span>...</span>");
                    }
                }
                //  sb.Append("</div>");

                if (pageIndex == totalPages)
                {
                    sb.AppendFormat("<span class='number'>{0}</span>", "下一页");
                }
                else
                {
                    dict[currentPageStr] = pageIndex + 1;
                    sb.AppendFormat("<span class='number'>{0}</span>", html.RouteLink("下一页", dict));
                }

                dict[currentPageStr] = totalPages;
                sb.AppendFormat("<span class='number'>{0}</span>", html.RouteLink("末页", dict));
            }
            return new MvcHtmlString(sb.ToString());
        }
    }
}